﻿Imports DevExpress.XtraEditors
Imports DevExpress.XtraSplashScreen
Imports IControl.IControls

Public Class NhapHang
    Private m_MaPhieu As String
    Private m_Row As DataRow
    Private m_DoanhThu, m_Vat, m_TienThue, m_TongCong As Decimal
    Public Sub setMa(ByVal ma As String)
        Me.m_MaPhieu = ma
    End Sub
    Public Overrides Sub setSubSau_TaoMoi()
        Me.setMacDinhCotAn("ma", "NHDP" & System.Guid.NewGuid.ToString)
        Me.TinhtrangISearchLookupEdit.EditValue = "DXL"
        Me.SochungtuITextEdit.EditValue = Me.QueriesTableAdapter1.getSoChungTu_KHHangHoa("NHDP", CDate(TuNgayIDateEdit.EditValue), CDate(DenNgayIDateEdit.EditValue)).ToString
        Me.NgaychungtuIDateEdit.EditValue = Today
        Me.NgayhoadonIDateEdit.EditValue = Today
    End Sub
    Private Sub NhapHang_Activated(sender As Object, e As EventArgs) Handles Me.Activated
        If Me.m_MaPhieu IsNot Nothing Then
            Dim position As Integer = Me.KHHangHoaBindingSource.Find("ma", Me.m_MaPhieu)
            Me.KHHangHoaBindingSource.Position = position
        End If
    End Sub
    Private Sub loadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.NghiepVuMuaHangTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.NghiepVuMuaHang)
        Me.DMThoiGianTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMThoiGian)
        Me.ThanhToanTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.ThanhToan)
        Me.DMHangHoaTableAdapter.Fill(Me.PCV_CLINICDataSet.DMHangHoa)
        Me.DMKhoTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMKho)
        Me.DMKhachHangTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMKhachHang)
        Me.KHHangHoaChiTietTableAdapter.Fill(Me.PCV_CLINICDataSet.KHHangHoaChiTiet, CDate(TuNgayIDateEdit.EditValue), CDate(DenNgayIDateEdit.EditValue))
        Me.KHHangHoaTableAdapter.Fill(Me.PCV_CLINICDataSet.KHHangHoa, CDate(TuNgayIDateEdit.EditValue), CDate(DenNgayIDateEdit.EditValue))
        Me.TinhTrangPhieuTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.TinhTrangPhieu)
        Me.KHThueTableAdapter.Fill(Me.PCV_CLINICDataSet.KHThue)
        Me.IGridControlDs01.ForceInitialize()
        Me.IGridViewDs01.MoveLast()
        Me.setHeaderFooter(Main.TenDoanhNghiep.ToString, Main.DiaChiDoanhNghiep.ToString)
        SplashScreenManager.CloseForm()
    End Sub
    Private Sub NhapHang_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.DMThoiGianISearchLookupEdit.EditValue = "TN"
        loadDuLieu()
    End Sub
    Public Sub New()
        InitializeComponent()
        Me.setSoTabThamChieu(0)
        Me.setSoTab(2)
        Me.setObj(Me.PCV_CLINICDataSet, Me.PCV_CLINICDataSet.KHHangHoa, Me.KHHangHoaBindingSource, Me.TableAdapterManager, Me.KHHangHoaChiTietTableAdapter, Me.ManghiepvuISearchLookupEdit, Me.TinhtrangISearchLookupEdit)
        Me.setTab1(Me.PCV_CLINICDataSet.KHHangHoaChiTiet, Me.FKKHHangHoaChima42E1EEFEBindingSource, Me.KHHangHoaChiTietTableAdapter)
        Me.setTab2(Me.PCV_CLINICDataSet.KHThue, Me.FKKHThuema44CA3770BindingSource, Me.KHThueTableAdapter)
        Me.setColumnsChangeTab1(New ArrayList() From {"soluonggiaodich", "dongiavnd", "tilethueGTGT", "giavonnhap"})
        Me.setColumnsChangeTab2(New ArrayList() From {"doanhthu", "phantramthue", "tienthue", "tongcong"})
        Me.setColumnValueTab1(Me.colEx1ChiTiet01, Me.colthanhtienvnd)
        Me.setColumnValueTab1(Me.colEx2ChiTiet01, Me.colthanhtiennhap)
        Me.setColumnValueTab1(Me.colEx3ChiTiet01, Me.coltienthueGTGT)
        Me.setColumnValueTab2(Me.colEx1ChiTiet02, Me.coltienthue)
        Me.setColumnValueTab2(Me.colEx2ChiTiet02, Me.coltongcong)
        Me.IGridViewChiTiet01.setBatLoi("mahang", "not null")
        Me.IGridViewChiTiet01.setBatLoi("solo", "not null")
        Me.IGridViewChiTiet01.setBatLoi("hsd", "not null")
        Me.IGridViewChiTiet01.setBatLoi("soluonggiaodich", "not 0 and not null")
        Me.IGridViewChiTiet01.setBatLoi("dongiavnd", "not 0 and not null")
        Me.IGridViewChiTiet01.setBatLoi("thanhtienvnd", "not 0 and not null")
        Me.IGridViewChiTiet01.setBatLoi("giavonnhap", "not 0 and not null")
        Me.IGridViewChiTiet01.setBatLoi("thanhtiennhap", "not 0 and not null")
        Me.IGridViewChiTiet02.setBatLoi("doanhthu", "not 0 and not null")
        Me.IGridViewChiTiet02.setBatLoi("phantramthue", "not 0 and not null")
        Me.IGridViewChiTiet02.setBatLoi("tienthue", "not 0 and not null")
        Me.IGridViewChiTiet02.setBatLoi("tongcong", "not 0 and not null")
    End Sub
    Private Sub RepositoryItemSearchLookUpEdit_DMHangHoa_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemSearchLookUpEdit_DMHangHoa.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("mahang", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        IGridViewChiTiet01.SetFocusedRowCellValue("tenhang", editor.Properties.View.GetRowCellValue(rowHandle, "ten"))
        IGridViewChiTiet01.SetFocusedRowCellValue("dvt", editor.Properties.View.GetRowCellValue(rowHandle, "donvitinh"))
    End Sub
    Private Sub IGridViewChiTiet01_InitNewRow(sender As Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewChiTiet01.InitNewRow
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("uid", System.Guid.NewGuid)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("sochungtu", Me.SochungtuITextEdit.EditValue)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("soluonggiaodich", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("dongiavnd", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("thanhtienvnd", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("giavonnhap", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("thanhtiennhap", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("tilethueGTGT", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("tienthueGTGT", 0)
        Me.setFlagInitNewRowTab1(True)
    End Sub
    Private Sub MadoituongISearchLookupEdit_EditValueChanging(sender As Object, e As Controls.ChangingEventArgs) Handles MadoituongISearchLookupEdit.EditValueChanging
        Me.TendoituongITextEdit.EditValue = Me.MadoituongISearchLookupEdit.getRows.Item("ten")
        Me.DiachiITextEdit.EditValue = Me.MadoituongISearchLookupEdit.getRows.Item("diachi")
        m_Row = getRow(Me.PCV_CLINICDataSet_Don.NghiepVuMuaHang, NghiepVuMuaHangBindingSource, "ma", Me.ManghiepvuISearchLookupEdit.EditValue)
        Me.DiengiaiITextEdit.EditValue = m_Row.Item("diengiai").ToString & " " & TendoituongITextEdit.EditValue
    End Sub
    Private Sub IGridViewChiTiet01_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles IGridViewChiTiet01.CellValueChanged
        Me.TienvndITextEdit.EditValue = Me.IGridViewChiTiet01.getSummaryColumn("thanhtienvnd")
    End Sub
    Private Sub DMThoiGianISearchLookupEdit_EditValueChanged(sender As Object, e As EventArgs) Handles DMThoiGianISearchLookupEdit.EditValueChanged
        setDMThoiGian(Me.DMThoiGianISearchLookupEdit, TuNgayIDateEdit, DenNgayIDateEdit)
        Me.BarButtonItemLamTuoi.PerformClick()
    End Sub
    Public Overrides Sub setSubTruoc_Luu()
        Me.setSuaTruocKhiLuuTab1("ngaychungtu", Me.NgaychungtuIDateEdit.EditValue)
        Dim row As DataRow = getRow(Me.PCV_CLINICDataSet_Don.NghiepVuMuaHang, NghiepVuMuaHangBindingSource, "ma", Me.ManghiepvuISearchLookupEdit.EditValue)
        Me.setSuaTruocKhiLuuTab1("taikhoanno", row.Item("taikhoanno").ToString)
        Me.setSuaTruocKhiLuuTab1("taikhoanco", row.Item("taikhoanco").ToString)
        Me.setSuaTruocKhiLuuTab1("taikhoanthueGTGT", row.Item("taikhoanvat").ToString)
        Me.setSuaTruocKhiLuuTab1("kyketoan", getRecycleId(CDate(Me.NgaychungtuIDateEdit.EditValue)))
    End Sub
    Protected Overrides Sub BarButtonItemLamTuoi_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs)
        Dim position As Integer = Me.KHHangHoaBindingSource.Position
        loadDuLieu()
        Me.KHHangHoaBindingSource.Position = position
    End Sub
    Private Sub RepositoryItemISearchLookUpEdit_DMHangHoa_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemISearchLookUpEdit_DMHangHoa.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("mahang", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        IGridViewChiTiet01.SetFocusedRowCellValue("tenhang", editor.Properties.View.GetRowCellValue(rowHandle, "ten"))
        IGridViewChiTiet01.SetFocusedRowCellValue("dvt", editor.Properties.View.GetRowCellValue(rowHandle, "donvitinh"))
        IGridViewChiTiet01.SetFocusedRowCellValue("quicach", editor.Properties.View.GetRowCellValue(rowHandle, "quicach"))
    End Sub
    Private Shadows Sub BarButtonItemBaoCao_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemBaoCao.ItemClick
        Dim report As Report_PhieuNhapKho = New Report_PhieuNhapKho
        report.KhHangHoaTableAdapter1.FillMa_KHHangHoa(report.PcV_CLINICDataSet1.KHHangHoa, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        report.KhHangHoaChiTietTableAdapter1.FillMa_KHHangHoaChiTiet(report.PcV_CLINICDataSet1.KHHangHoaChiTiet, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        report.NgayThangNam.Value = "Ngày " & Format(CDate(Me.NgaychungtuIDateEdit.EditValue), "dd") & " Tháng " & Format(CDate(Me.NgaychungtuIDateEdit.EditValue), "MM") & " Năm " & Format(CDate(Me.NgaychungtuIDateEdit.EditValue), "yyyy")
        report.ShowRibbonPreview()
    End Sub
    Private Sub ChuyểnDữLiệuThuếToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ChuyểnDữLiệuThuếToolStripMenuItem.Click
        If Me.getState <> 0 Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn chuyển dữ liệu thuế không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.TabbedControlGroupChiTiet.SelectedTabPage = Me.TabbedControlGroupChiTiet.TabPages(1) 'chọn tab thue
                Me.IGridViewChiTiet02.AddNewRow()
            End If
        End If
    End Sub
    Private Sub IGridViewChiTiet02_InitNewRow(sender As Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewChiTiet02.InitNewRow
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("sochungtu", Me.SochungtuITextEdit.EditValue)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("ngaychungtu", Me.NgaychungtuIDateEdit.EditValue)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("sohoadon", Me.SohoadonITextEdit.EditValue)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("ngayhoadon", Me.NgayhoadonIDateEdit.EditValue)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("doanhthu", CDec(Me.IGridViewChiTiet01.getSummaryColumn("thanhtienvnd")))
        Dim row As DataRow = getRow(Me.PCV_CLINICDataSet_Don.NghiepVuMuaHang, NghiepVuMuaHangBindingSource, "ma", Me.ManghiepvuISearchLookupEdit.EditValue)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("phantramthue", CInt(row.Item("tylevat")))
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("tienthue", CInt(row.Item("tylevat")) * CDec(Me.IGridViewChiTiet01.getSummaryColumn("thanhtienvnd")) / 100)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("tongcong", CInt(row.Item("tylevat")) * CDec(Me.IGridViewChiTiet01.getSummaryColumn("thanhtienvnd")) / 100 + CDec(Me.IGridViewChiTiet01.getSummaryColumn("thanhtienvnd")))
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("uid", System.Guid.NewGuid)
        Me.setFlagInitNewRowTab2(True)
    End Sub
End Class
